package com.itinsys.enma.web;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itinsys.enma.domain.User;
import com.itinsys.enma.domain.response.UserResponse;
import com.itinsys.enma.manager.UserManager;

@Controller
@RequestMapping({ "/user" })
public class UserFormController extends FormController {
	protected final Log logger = LogFactory.getLog("controller");

	@Autowired
	private UserManager userManager;


	@RequestMapping(value = { "/assignRoleToUser" }, method = { RequestMethod.POST })
	@ResponseBody
	public UserResponse assignRolToUser(@RequestBody Map<String, String> userRolDTO,HttpServletResponse response) {

		final String userName = (String) userRolDTO.get("userName");
		final String strRole =(String) userRolDTO.get("role");

		return userManager.assignRolToUser(userName, strRole);
	}

	@RequestMapping(value = { "/updateRoleToUser" }, method = { RequestMethod.POST })
	@ResponseBody
	public UserResponse updateRoleToUser(@RequestBody User user,HttpServletResponse response) {
		return userManager.updateRoleToUser(user.getUsername(), user.getRole().getId());
	}
	
	@RequestMapping(value = { "/delete" }, method = { RequestMethod.POST })
	@ResponseBody
	public UserResponse delete(@RequestBody Map<String, String> userRolDTO,HttpServletResponse response) {
		return userManager.delete((String) userRolDTO.get("id"));
	}
	
	@RequestMapping(value = "/getAll/", method = RequestMethod.GET)
	@ResponseBody
	public UserResponse getAllUsers() {
		return userManager.getAll();
		
	}
	
	
}
